<template>
  <div class="apply">
    <div class="d-flex justify-content-between">
      <div></div>
      <div class="rightBar">
        <el-dialog
            title="发布精彩记录图片"
            :visible.sync="dialogVisiblePush"
            width="50%">
          <el-upload
              class="upload-img"
              action="/api/Admin/upload/img"
              :on-remove="delFile"
              :on-success="onSuccess"
              name="img"
              :limit="1"
              :on-exceed="handleExceed"
              :before-upload="beforeUpload"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过1024kb</div>
          </el-upload>
          <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisiblePush = false">取 消</el-button>
              <el-button type="primary" @click="insertIntoOk">确 定</el-button>
          </span>
        </el-dialog>
        <el-button @click=" dialogVisiblePush = true" type="primary" icon="el-icon-plus" circle></el-button>
        <el-popover
            placement="top"
            width="160"
            v-model="visible">
          <p>确定全部删除吗？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="text" @click="visible = false">取消</el-button>
            <el-button type="primary" size="mini" @click="handleClickDel(undefined)">确定</el-button>
          </div>
          <el-button slot="reference" type="danger" icon="el-icon-delete" data-bs-toggle="tooltip"
                     data-bs-placement="top"
                     title="全部删除" circle></el-button>
        </el-popover>

      </div>
    </div>
    <div class="applyBody">
      <el-table
          ref="singleTable"
          :data="advData"
          highlight-current-row
          :header-cell-style="{'text-align':'center'}"
          :cell-style="cellStyle"
          border
          style="width: 100%;background-color: white">
        <el-table-column
            type="index"
            label="序号"
            width="50">
        </el-table-column>
        <el-table-column
            label="图片"
            width="200">
          <template slot-scope="scope">
            <img class="img-thumbnail" :src="'http://127.0.0.1/'+JSON.parse(scope.row.path).path" alt="">
          </template>
        </el-table-column>

        <el-table-column
            label="创建时间"
            width="250">
          <template slot-scope="scope">
            {{ scope.row.createTime|time }}
          </template>
        </el-table-column>

        <el-table-column
            label="更新时间"
            width="250">
          <template slot-scope="scope">
            {{ scope.row.updateTime|time }}
          </template>
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClickDel(scope.row._id)" type="danger" size="small">删除</el-button>

          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          class="mt-3"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[30, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import admin from "../../../api/admin";
import time from "../../../filter/time";

export default {
  name: "Wonderful",
  data() {
    return {
      visible: false,
      advData: [],
      filePath: [],
      dialogVisiblePush: false, limit: 30,
      skip: 0,
      page: 1,
      size: 30,
      total: 1,
    }
  },
  mounted() {
    this.GetAdv()
  },
  filters: {
    time
  },
  methods: {
    handleClickDel(id) {
      this.visible = false
      const param = {
        id
      }
      const loading = this.$loading({
        lock: true,
        text: '删除中...请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      admin.delAdv(param).then(r => {
        if (r.data.status === '200') {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.GetAdv()
          loading.close()
        }
      }).catch(() => {
        this.$message.error('删除失败');
        loading.close()
      })
    },
    handleSizeChange(size) {
      this.size = size
      this.limit = size
      this.GetAdv()
    },
    handleCurrentChange(size) {
      this.page = size
      this.GetAdv()
    },
    insertIntoOk() {
      const loading = this.$loading({
        lock: true,
        text: '添加中请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      const param = {
        fileInfo: this.filePath
      }
      admin.AddAdv(param).then(r => {
        this.dialogVisiblePush = false
        if (r.data.status === '200') {
          this.$message({
            message: '上传成功',
            type: 'success'
          });
          this.GetAdv()
          loading.close()
        }
      }).catch(() => {
        this.$message.error('删除失败');
        loading.close()
      })
    },
    GetAdv() {
      const param = {
        limit: this.limit,
        skip: (this.page - 1) * this.size,
      }
      admin.GetAdv(param).then(r => {
        this.advData = r.data.data.adv
      })
    },
    //删除文件
    delFile(file) {
      admin.DelUploadFile({path: file.response.data.fileInfo.path}).then()
    },
    // 文件上传成功
    onSuccess(res) {
      this.filePath.push(res.data.fileInfo)
    },
    // 超出文件指定个数的钩子函数
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    // 上传之前判断文件大小的路由钩子
    beforeUpload(file) {
      if (file.size / 1024 > 1024) {
        this.$alert('上传的文件不能大于500kb', '温馨提示', {
          confirmButtonText: '确定'
        });
        return false
      }
    }, cellStyle() {
      return {
        'text-align': 'center',
        'overflow': 'hidden',
        'text-overflow': 'ellipsis',
        'white-space': 'nowrap',
      }
    },
  }
}
</script>

<style scoped>

</style>